package com.chinalanshen.mqtt.config;

import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;

import com.chinalanshen.mqtt.service.GetMessage;

@Configuration
@IntegrationComponentScan
public class EmqConfig {
 
	@Autowired
	private FusionEmqProperties fusionEmqProperties;
	
    @Bean
    public MqttPahoClientFactory mqttClientFactory() {
        MqttConnectOptions mqttConnectOptions=new MqttConnectOptions();
        mqttConnectOptions.setUserName(fusionEmqProperties.getUsername());
        mqttConnectOptions.setPassword(fusionEmqProperties.getPassword().toCharArray());
        mqttConnectOptions.setServerURIs(new String[]{fusionEmqProperties.getUrl()});
        mqttConnectOptions.setKeepAliveInterval(2);
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        factory.setConnectionOptions(mqttConnectOptions);
        return factory;
    }
    
	@Bean
	@ServiceActivator(inputChannel = "mqttOutboundChannel")
	public MessageHandler mqttOutbound() {
		MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(fusionEmqProperties.getClientId(), mqttClientFactory());
		messageHandler.setAsync(true);
		messageHandler.setDefaultTopic("hello");
		return messageHandler;
	}
	
    @Bean
    public MessageChannel mqttOutboundChannel() {
        return new DirectChannel();
    }
    
    //接收通道
    @Bean
    public MessageChannel mqttInputChannel() {
        return new DirectChannel();
    }
 
    //配置client,监听的topic 
    @Bean
    public MessageProducer inbound() {
    	String[] topic = fusionEmqProperties.getTopic();
        MqttPahoMessageDrivenChannelAdapter adapter =new MqttPahoMessageDrivenChannelAdapter(fusionEmqProperties.getClientId()+"_inbound", mqttClientFactory(),topic);
        adapter.setCompletionTimeout(fusionEmqProperties.getTimeout());
        adapter.setConverter(new DefaultPahoMessageConverter());
        adapter.setQos(1);
        adapter.setOutputChannel(mqttInputChannel());
        return adapter;
    }
 
    //通过通道获取数据
    @Bean
	@ServiceActivator(inputChannel = "mqttInputChannel")
    public MessageHandler handler() {
        return new GetMessage();
    }
}